;;; my-setting.el --- A listing of modules to load on startup
;;

;;; Commentary:


;; weather
(defun my/weather ()
  "天气预报 based on https://github.com/chubin/wttr.in"
  (interactive)
  (eww "zh-cn.wttr.in/qingdao,laoshan?TAFm")) ;;qingdao,laoshan?TAFm 带区县也是可以

;; M-x user
(defun my/user()
  (interactive)
  ( insert (concat "Author: "
                   (user-login-name) "\n"
                   "Date: "
                   (current-time-string)
                   )))

;; 复制当前行
(defun my/copy-line ()
  (interactive)
  (save-excursion
    (back-to-indentation)
    (kill-ring-save
     (point)
     (line-end-position)))
  (message "1 line copied"))

;; todo关键词 上色
(add-hook 'prog-mode-hook
          (lambda ()
            (font-lock-add-keywords
             nil '(("\\<\\(FIXME\\|DEBUG\\|TODO\\):"
                    1 font-lock-warning-face prepend)))))
;; show todo list
(defun my/show-todo-list()
  "show todo list"
  (interactive)
  (project-find-regexp "TODO")
  )
;; 显示标题栏文件路径
(defun my/show-file-name ()
  "Show the full path file name in the minibuffer."
  (interactive)
  (message (buffer-file-name))
  (kill-new (file-truename buffer-file-name))
  )
;;eww
(setq
 browse-url-browser-function 'eww-browse-url ; Use eww as the default browser
 shr-use-fonts  nil                          ; No special fonts
 shr-use-colors nil                          ; No colours
 shr-indentation 2                           ; Left-side margin
 shr-width 70                                ; Fold text to 70 columns
)

;; autoload
(defun check-run-command (command arg-string)
  "FIND the EXEC-FILE and RUN the BODY.  COMMAND ARG-STRING."

  (if (not (executable-find command))
      (message "[ERROR]: <%s> not found!" command)
    (save-buffer)
    (shell-command (format "%s %s" command arg-string))
    (revert-buffer t t t)))

(defun python-isort ()
  "Sort the imports with isort."
  (interactive)
  (check-run-command "isort"
		             (format "--atomic --profile=black %s"
			                 (buffer-file-name))))

;; BE CAREFUL! Maybe bugs here, always call this function manually.
;; autoload
(defun python-remove-all-unused-imports ()
  "Remove all the unused imports, do NOT use pyimport, as it has bugs.
eg.from datetime import datetime."
  (interactive)
  (check-run-command "autoflake"
		             (format "-i --remove-all-unused-imports %s"
			                 (buffer-file-name))))

(add-hook 'python-mode-hook
          (lambda ()
            (define-key python-mode-map (kbd "C-c j s") 'python-isort)
            (define-key python-mode-map (kbd "C-c j r") 'python-remove-all-unused-imports)))

;; 打开默认emacs.d 目录
(defun my/open-emacs-dir()
  "Open myown emacs.d directory config file."
  (interactive)
  (dired "~/.emacs.d/"))

(global-set-key [f2] 'my/open-emacs-dir) ;;打开emacs.d配置 init.el文件

(global-set-key (kbd "C-c [") 'project-find-file) ;;项目中查找文件
(global-set-key (kbd "C-c ,") 'my/user) ;; user - Date
(global-set-key (kbd "C-c ;") 'crux-duplicate-and-comment-current-line-or-region) ;;复制注释当前行
(global-set-key (kbd "C-c b") 'blink-search) ;;blink-search

(global-set-key (kbd "C-c j d") 'my/copy-line) ;; duplicate-line ;; (M-y) 粘贴
(global-set-key (kbd "C-c j f")  'format-all-buffer) ;;格式化代码（prettier）
(global-set-key (kbd "C-c j t") 'my/show-todo-list) ;;展示所有todo标签
(global-set-key (kbd "C-c j z") 'my/show-file-name) ;; 展示本文件物理地址
(global-set-key (kbd "C-c j w") 'my/weather) ;;查看天气
(global-set-key (kbd "C-c j o") 'crux-smart-open-line) ;;当前行下插入一行
(global-set-key (kbd "C-c j a") 'achive) ;;achive
(global-set-key (kbd "C-c j j")  'recentf-open-files) ;;recentf-open-files
;; sort-tab
; (global-set-key (kbd "M-1") 'sort-tab-select-visible-tab)
; (global-set-key (kbd "M-2") 'sort-tab-select-visible-tab)
; (global-set-key (kbd "M-3") 'sort-tab-select-visible-tab)
; (global-set-key (kbd "M-4") 'sort-tab-select-visible-tab)
; (global-set-key (kbd "M-5") 'sort-tab-select-visible-tab)
; (global-set-key (kbd "M-6") 'sort-tab-select-visible-tab)
; (global-set-key (kbd "M-7") 'sort-tab-select-visible-tab)
; (global-set-key (kbd "M-8") 'sort-tab-select-visible-tab)
; (global-set-key (kbd "M-9") 'sort-tab-select-visible-tab)
; (global-set-key (kbd "M-]") 'sort-tab-close-other-tabs)
; (global-set-key (kbd "M-0") 'sort-tab-close-current-tab)

(provide 'my-setting)